package com.me.brainlessbirds.actors.survivors;

import java.util.ArrayList;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Timer.Task;
import com.me.brainlessbirds.actors.GameObject;
import com.me.brainlessbirds.actors.Unit;
import com.me.brainlessbirds.actors.obstacles.Obstacle;
import com.me.brainlessbirds.actors.obstacles.units.Barricade;
import com.me.brainlessbirds.actors.skills.BaseAttack;
import com.me.brainlessbirds.actors.survivors.units.Civilian;
import com.me.brainlessbirds.actors.survivors.units.Engineer;
import com.me.brainlessbirds.actors.survivors.units.Flamethrower;
import com.me.brainlessbirds.actors.survivors.units.Soldier;
import com.me.brainlessbirds.actors.zombies.Zombie;
import com.me.brainlessbirds.actors.zombies.units.Brainless;
import com.me.brainlessbirds.actors.zombies.units.Smoothie;
import com.me.brainlessbirds.actors.zombies.units.Undertaker;
import com.me.brainlessbirds.actors.zombies.units.Winehouse;
import com.me.brainlessbirds.screens.GameScreen;

public abstract class Survivor extends Unit
{
	//gg !
	public enum SurvivorType
	{
		CIVILIAN, ENGINEER, FLAMETHROWER, SOLDIER;
	}

	protected SurvivorType	type		= null;
	protected int			incomeValue	= 0;

	public void start(Stage stage, float x, float y)
	{
		rgn = rgnIdle;
		setPosition(x, y - (getHeight() / 2));
		addMoveToLeft(stage);
	}

	public void addMoveToLeft(Stage stage)
	{
		super.addMoveToLeft(stage, new Task()
		{
			@Override
			public void run()
			{
				escape();
			}
		});
	}

	public void escape()
	{
		if (GameScreen.get().survivors.remove(this))
		{
			GameScreen.get().survivorEscaped();
			GameScreen.get().createParticleEffect(Survivor.this.getX() + Survivor.this.getWidth() / 2, Survivor.this.getY() + Survivor.this.getHeight() / 2);
			Survivor.this.remove();
		}
	}

	public void destroy()
	{
		super.destroy(new Task()
		{
			@Override
			public void run()
			{
				GameScreen.get().createParticleEffect(Survivor.this.getX() + Survivor.this.getWidth() / 2, Survivor.this.getY() + Survivor.this.getHeight() / 2);
				Survivor.this.remove();
			}
		});
	}

	public int getIncomeValue()
	{
		return this.incomeValue;
	}

	@Override
	public boolean collideLeft(GameObject actor)
	{
		boolean isColliding = super.collideLeft(actor);
		if (isColliding)
		{
			if (actor instanceof Survivor)
			{
				//this.setBlocked(true);
			}
			else if (actor instanceof Obstacle)
			{
				Obstacle o = (Obstacle) actor;
				if (o instanceof Barricade && !o.isDown())
				{
					this.setBlocked(true);
				}
				if (this instanceof Engineer && this.getX() - actor.getX() < 20f)
				{
					Engineer e = (Engineer) this;
					if (e.canRepare && o.isDown())
					{
						e.canRepare = false;
						o.repare();
					}
				}
			}
			else if (actor instanceof Zombie)
			{
				this.setBlocked(true);
				if (this instanceof Engineer || this instanceof Flamethrower || this instanceof Soldier)
				{
					rgn = rgnAtk;
				}
			}
		}

		return isColliding;
	}

	// Zombies

	@Override
	public void OnHitByBrainless(Brainless _b)
	{
		ArrayList<GameObject> targetsArrayList = new ArrayList<GameObject>();
		targetsArrayList.add(this);
		_b.skillMap.get(BaseAttack.class).launchEffect(targetsArrayList);
	}

	@Override
	public void OnHitBySmoothie(Smoothie _s)
	{
		ArrayList<GameObject> targetsArrayList = new ArrayList<GameObject>();
		targetsArrayList.add(this);
		_s.skillMap.get(BaseAttack.class).launchEffect(targetsArrayList);
	}

	@Override
	public void OnHitByUndertaker(Undertaker _u)
	{
		ArrayList<GameObject> targetsArrayList = new ArrayList<GameObject>();
		targetsArrayList.add(this);
		_u.skillMap.get(BaseAttack.class).launchEffect(targetsArrayList);

	}

	@Override
	public void OnHitByWinehouse(Winehouse _w)
	{
		ArrayList<GameObject> targetsArrayList = new ArrayList<GameObject>();
		targetsArrayList.add(this);
		_w.skillMap.get(BaseAttack.class).launchEffect(targetsArrayList);

	}

	//Survivors

	@Override
	public void OnHitByCivilian(Civilian _c)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void OnHitByEngineer(Engineer ec)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void OnHitByFlamethrower(Flamethrower _f)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void OnHitBySoldier(Soldier _s)
	{
		// TODO Auto-generated method stub

	}
}
